package com.youlu.campus.web.intergral.service;

import com.youlu.campus.base.base.PageQueryRequest;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.intergral.UserIntegralLogPO;
import com.youlu.campus.web.intergral.dto.UserIntegralLogCreateDTO;
import com.youlu.campus.web.intergral.dto.UserIntegralLogResponse;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by zgz on 2020/11/8.
 */
@Service
@Slf4j
public class UserIntegralLogService extends BaseService {

    @Autowired
    private MapperFacade mapperFacade;

    public void createUserIntegralLog(UserIntegralLogCreateDTO createDTO) {
        UserIntegralLogPO logPO = mapperFacade.map(createDTO, UserIntegralLogPO.class);
        logPO.setCreatedTime(new Date());
        this.mongoTemplate.save(logPO);
    }

    public Page<UserIntegralLogResponse> getUserIntegralLog(PageQueryRequest request) {
        log.info("getUserIntegralLog request {}", request);
        Query query = new Query();
        query.addCriteria(Criteria.where("deleted").is(false));
        query.addCriteria(Criteria.where("userId").is(RequestContext.getUserId()));
        long count = mongoTemplate.count(query, UserIntegralLogPO.class);
        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "id"));
        Sort sort=Sort.by(Sort.Direction.DESC, "id");
        query.with(sort);
        List<UserIntegralLogPO> logPOList = mongoTemplate.find(query.with(pageRequest), UserIntegralLogPO.class);
        List<UserIntegralLogResponse> list = logPOList.stream().map(logPO -> {
            UserIntegralLogResponse response = new UserIntegralLogResponse();
            BeanUtils.copyProperties(logPO, response);
            response.setLogDesc(getBizType(logPO.getBusinessType()));
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    private String getBizType(Integer i) {
        if (Objects.isNull(i)) {
            return "其他";
        }
        switch (i) {
            case 1:
                return "邀请赠送积分";
            case 2:
                return "分享好友";
            case 3:
                return "给他人点赞";
            case 4:
                return "给他人评论";
            case 5:
                return "兑换一次补卡机会";
            default:
                return "其他";
        }

    }
}
